package com.example.automanagement.controller;

import com.example.automanagement.entity.AutoArchives;
import com.example.automanagement.entity.AutoDepartmentApproval;
import com.example.automanagement.entity.AutoGeneralApproval;
import com.example.automanagement.service.*;
import com.example.automanagement.util.Result;
import com.example.automanagement.views.AutoArchiveStatus;
import com.example.automanagement.views.DriverArchiveStatus;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
public class AutoApprovalController {

    @Resource
    private AutoApprovalService autoApprovalService;
    @Resource
    private AutoStatusService autoStatusService;
    @Resource
    private DriverStatusService driverStatusService;

    @Resource
    private ArchiveService archiveService;
    //插入部门审批
    @PostMapping("/addAutoDPApproval")
    public Result addAutoDPApproval(@RequestBody AutoDepartmentApproval approval){
        if(approval == null)
            return Result.error("500", "请检查数据");
        autoApprovalService.addAutoDepartmentApproval(approval);
        return Result.success();
    }

    //TODO 有参查询车牌号（视图） 参数：车型、状态（隐）插入车辆档案时也需要插入车辆状态表
    //TODO 有参查询驾驶员（视图） 参数：状态（隐） 插入驾驶员档案时也需要插入驾驶员状态表
    //TODO 完成调度之后修改驾驶员和车辆状态为busy

    //插入综合办审批
    @PostMapping("/addAutoGeneralApproval")
    public Result addAutoGeneralApproval(@RequestBody AutoGeneralApproval approval){
        if(approval == null)
            return Result.error("500","请检查数据");

        autoApprovalService.addAutoGeneralApproval(approval);

        AutoArchives autoArchive = archiveService.getAutoIdByPlate(approval.getAutoPlateNum());

        //同时修改车辆和驾驶员状态
        autoStatusService.updateAutoBusy(autoArchive.getAutoId());
        driverStatusService.updateDriverBusy(approval.getDriverId());
        return Result.success();
    }

    @GetMapping("/queryFreePlateNum")
    public Result queryFreePlateNum(@RequestParam("autoType") String autoType){
        if(autoType == null)
            return Result.error("500", "请检查数据");
        //有参查询车牌号（视图） 参数：车型、状态（隐）
        List<AutoArchiveStatus> freePlateNums = autoApprovalService.getFreePlateNum(autoType);
        return Result.success(freePlateNums);
    }

    @GetMapping("/queryFreeDrivers")
    public Result queryFreeDrivers(){
        //有参查询驾驶员（视图） 参数：状态（隐）
        List<DriverArchiveStatus> freeDrivers = autoApprovalService.getFreeDrivers();
        return Result.success(freeDrivers);

    }

    //获取综合办审批数据
    @GetMapping("/getAutoGeneralApproval")
    public Result getAutoGeneralApproval(@RequestParam("orderId") String orderId){
        AutoGeneralApproval approval = autoApprovalService.getAutoGeneralApproval(orderId);
        if(approval == null){
            return Result.error("500","查询失败");
        }
        return Result.success(approval,"查询成功");
    }

    //获取部门审批数据
    @GetMapping("/getAutoDepartmentApproval")
    public Result getAutoDepartmentApproval(@RequestParam("orderId")String orderId){
        AutoDepartmentApproval approval = autoApprovalService.getAutoDepartmentApproval(orderId);
        if (approval == null){
            return Result.error("500","查询失败");
        }
        return Result.success(approval,"查询成功");
    }
}
